﻿using DataService.Model;
using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataService.Helper
{
    public class OracleHelper
    {
        LogHelper logHelper = new LogHelper();
        //static string connectionString = "User ID=admin;Password=admin123;Data Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = 10.127.100.189)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = ORACLEDATA)))";
        string connectionString = "user id=COLLECTION;password=Collection!zh801;data source=//172.20.0.21:1521/MESZH;Pooling=true;Min Pool Size=1";

        OracleConnection conn;
        public OracleHelper()
        {
            conn = new OracleConnection(connectionString);
        }
        public OracleHelper(string connectionString)
        {
            conn = new OracleConnection(connectionString);
        }
        /// <summary>
        /// 打开连接
        /// </summary>
        /// <returns></returns>
        public bool Open()
        {
            try
            {
                conn.Open();
                return true;
            }
            catch
            {
                if (conn.State == System.Data.ConnectionState.Open)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        /// <summary>
        /// 关闭连接
        /// </summary>
        /// <returns></returns>
        public bool Close()
        {
            try
            {
                conn.Close();
                return true;
            }
            catch
            {
                if (conn.State == System.Data.ConnectionState.Closed)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public int ExecuteNonQuery(string sql, params OracleParameter[] parameters)
        {
            try
            {
                using (OracleCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(parameters);
                    int i = cmd.ExecuteNonQuery();
                    return i;
                }
            }
            catch (Exception ex)
            {
                logHelper.Error($"{parameters[0].Value}-{sql}" + "\r\n" + ex.Message + "\r\n" + ex.StackTrace);
            }
            return 0;
        }

        public DataTable ExecuteDataTable(string sql, params OracleParameter[] parameters)
        {
            using (OracleCommand cmd = conn.CreateCommand())
            {
                cmd.CommandText = sql;
                cmd.Parameters.AddRange(parameters);
                OracleDataAdapter adapter = new OracleDataAdapter(cmd);
                DataTable datatable = new DataTable();
                adapter.Fill(datatable);
                return datatable;
            }
        }
    }
}
